package com.shakev.coupon.es.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shakev.coupon.common.util.JsonUtils;
import com.shakev.coupon.es.service.EGoodsDetailService;
import com.shakev.coupon.taobao.model.GoodsDetail;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.*;
import com.taobao.api.response.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName TkService
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-05-13 下午 5:20
 * @UpdateDate 2019-05-13 下午 5:20
 * @Version 1.0
 */
@Component
public class TkService {


    @Resource
    EGoodsDetailService goodsDetailService;

//    req.setAdzoneId(108863900396L);
    public static void main(String[] args) throws ApiException {
        execItemSimple();
//        execItem();
//        execwuliao();
//        execCoupon();

    }

    public static void execwuliao() throws ApiException {
                TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest"
                , "27423545"
                , "c4f5afd47d1aac4f4d35300b341d503f");
        TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
        req.setPageSize(2L);
        req.setAdzoneId(108863900396L);
        req.setPageNo(1L);
        req.setMaterialId(Long.valueOf(3767));
//        req.setMaterialId(3756L);
//        req.setDeviceValue("xxx");
//        req.setDeviceEncrypt("MD5");
//        req.setDeviceType("IMEI");
//        req.setContentId(323L);
//        req.setContentSource("xxx");
//        req.setItemId(33243L);
        TbkDgOptimusMaterialResponse rsp = client.execute(req) ;
        System.out.println(rsp.getBody());
        JSONObject jsonObject = JSON.parseObject(rsp.getBody());
        System.out.println(rsp.getBody());
//        JSONArray arr = jsonObject.getJSONObject("tbk_dg_optimus_material_response").getJSONObject("result_list").getJSONArray("map_data");
//        for(Iterator iterator = arr.iterator();iterator.hasNext();){
////            System.out.println(iterator.next().toString());
//            GoodsDetail goods = JSON.parseObject(iterator.next().toString(),GoodsDetail.class);
//            System.out.println(goods.getCategoryId()+","+goods.getCategoryName()+","+goods.getLevelOneCategoryId()+","+goods.getLevelOneCategoryName());
//        }
    }

    public static void execItemSimple() throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest"
                , "27423545"
                , "c4f5afd47d1aac4f4d35300b341d503f");
        TbkItemGetRequest req = new TbkItemGetRequest();
        req.setFields("product_prop_imgs,num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
        req.setQ("dior蓝金旗舰女999口红");

        req.setPlatform(2L);
        req.setPageNo(0L);
        req.setPageSize(20L);
        TbkItemGetResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
    }

    public static void execItem() throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest"
                , "27423545"
                , "c4f5afd47d1aac4f4d35300b341d503f");
        TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
        req.setNumIids("591591111588");
        req.setPlatform(2L);
        TbkItemInfoGetResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
    }

    public static void execCoupon() throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest"
                , "27423545"
                , "c4f5afd47d1aac4f4d35300b341d503f");
        TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();
        req.setAdzoneId(108863900396L);
        req.setPlatform(2L);
//        req.setCat("16,18");
        req.setQ("迪奥999口红蓝金旗舰唇膏女520 740官方店正品");
        TbkDgItemCouponGetResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
    }

    public  void exec(Long materialId,Long pageNo,Long pageSize) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest"
                , "27423545"
                , "c4f5afd47d1aac4f4d35300b341d503f");
        TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
        req.setPageSize(pageSize==null?20L:pageSize);
        req.setAdzoneId(108863900396L);
        req.setPageNo(pageNo==null?0L:pageNo);
        req.setMaterialId(materialId);
//        req.setDeviceValue("xxx");
//        req.setDeviceEncrypt("MD5");
//        req.setDeviceType("IMEI");
//        req.setContentId(323L);
//        req.setContentSource("xxx");
//        req.setItemId(33243L);
        TbkDgOptimusMaterialResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
        JSONObject jsonObject = JSON.parseObject(rsp.getBody());
        JSONArray arr = jsonObject.getJSONObject("tbk_dg_optimus_material_response").getJSONObject("result_list").getJSONArray("map_data");
        for(Iterator iterator = arr.iterator();iterator.hasNext();){
            System.out.println(iterator.next().toString());
            GoodsDetail goods = JSON.parseObject(iterator.next().toString(),GoodsDetail.class);
            goods.setMaterialId(materialId);
            System.out.println(goods.toString());
//            System.out.println(goods.toString());
            goodsDetailService.save(goods);
        }
    }


    public static String stringToAscii(String value)
    {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(i != chars.length - 1)
            {
                sbu.append((int)chars[i]).append(",");
            }
            else {
                sbu.append((int)chars[i]);
            }
        }
        return sbu.toString();
    }
}

